window.addAddress||(addAddress= {
    /**
     * 省，市，区
     * **/
    address: [
        {
            name: 'country',
            num: 1
        },
        {
            name: 'province',
            num: 0
        },
        {
            name: 'city',
            num: ''
        },
        {
            name: 'district',
            num: ''
        }],
    init: function (options) {
        var me = this;
        for(var i in options){
            var item=options[i];
        }
        if(options){
            me.address=[
                {
                    name: 'country',
                    num: 1
                },
                {
                    name: 'province',
                    num: options.ua_province_id
                },
                {
                    name: 'city',
                    num: options.ua_city_id
                },
                {
                    name: 'district',
                    num: options.ua_county_id
                }
            ];
        }
        me.initAddress();
    },
    modifyAddress: function (id, panel, callback) {
        var me = this;
        $.ajax({
            type: "POST",
            url: STATIC_CONFIG.WWWROOT+"/index/address/autoAddress",
            dataType: 'json',
            data: {
                "region_id": id
            },
            success: function (result) {
                if (result.code == 200) {
                    $(panel).html('');
                    $.each(result.message, function (i, item) {
                        $(panel).append('<option value="' + item.region_id + '">' + item.name + '</option>');
                    });
                    if (callback) callback(result.message);
                }
            }
        });
    },
    initAddress: function (indexs) {
        var me = this;
        if(!indexs){
            indexs=0;
        }
        var data=me.address[indexs],
            next=me.address[indexs+1];
        if(next) {
            var selectPanel=$('#' + next.name);
            me.modifyAddress(data.num, selectPanel, function (arr) {
                if (next) {
                    if (next.num != '') {
                        var cityStart = null;
                        $.each(arr, function (i, item) {
                            if (item.region_id == next.num) {
                                cityStart = item.region_id;
                                return false;
                            }
                        });
                        if (cityStart == null) {
                            next.num = arr[0].region_id;
                        }
                    } else {
                        next.num = arr[0].region_id;
                    }
                    selectPanel.val(next.num);
                    me.initAddress(indexs+1);
                }

                $.each(me.callBack,function (i,item) {
                    if(typeof item=='function'){
                        item(me);
                    }
                });
            });
        }
    },
    callBack:{},
    changeProvince: function (t) {
        var me = this,
            that = $(t);
        me.address[1].num = that.val();
        me.initAddress(1);
    },
    changeCity: function (t) {
        var me = this,
            that = $(t);
        me.address[2].num = that.val();
        me.initAddress(2);
    },
    changeDistrict: function (t) {
        var me = this,
            that = $(t);
        me.address[3].num = that.val();
        $.each(me.callBack,function (i,item) {
            if(typeof item=='function'){
                item(me);
            }
        });
    },
    getElement:function () {
        var me=this;
        me.DOEM={
            province:$("#province"),
            city:$("#city"),
            district:$("#district")
        };
    }
});

// addAddress.init();

